package com.example.chess.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 管理用户聊天的 webSocket
 */
@Component
public class ChatManager {
    // 将用户的id和会话 webSocket关联起来
    private ConcurrentHashMap<Integer, WebSocketSession> chatSessions = new ConcurrentHashMap<>();

    // 获取session
    public WebSocketSession getSession(int userId){
        return chatSessions.get(userId);
    }
    // 添加session
    public void enterChat(int userId, WebSocketSession session){
        chatSessions.put(userId, session);
    }
    // 移除
    public void outChat(int userId){
        chatSessions.remove(userId);
    }
}
